﻿using System.Collections.Generic;
using System.Configuration;
using System.Linq;

namespace Citi.OTFPC.Pep.Domain
{
    public class User
    {
        public string Soeid { get; set; }
        public string Name { get; set; }
        private readonly string XmlPath = ConfigurationManager.AppSettings["UsersXmlPath"];
        public string NetworkUser { get; set; }
        public string NetworkPassword { get; set; }

        public User SelectByName(string name)
        {
            return SelectAll().FirstOrDefault(x => x.Name == name);
        }

        public List<User> SelectAll()
        { 
            using (Helpers.NetworkShareAccesser.Access(XmlPath, NetworkUser, NetworkPassword))
                return Helpers.XmlHelper.DeserializeFromXml<List<User>>(XmlPath);
        }

        public void SaveAll(List<User> users)
        {
            Helpers.XmlHelper.SerializeToXml(users, XmlPath);
        }

        public void Create(User user)
        {
            var users = SelectAll();

            users.Add(user);

            using (Helpers.NetworkShareAccesser.Access(XmlPath, NetworkUser, NetworkPassword))
                Helpers.XmlHelper.SerializeToXml(users, XmlPath);
        }
    }
}
